#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
//http://oj33.cn/contest.php?cid=1075
//https://blog.csdn.net/qq_37482202/article/details/89513877

int G[100][100];
int v;
int main()
{
	
	int n, m;
	cin >> n >> v;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			string s;
			cin >> s;
			if (s == "-")G[i][j] = 1e9;
			else
				G[i][j] = stoi(s);

		
		}
	}


	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			for (int k = 0; k < n; k++)
			{
				G[j][k] = min(G[j][k], G[j][i] + G[i][k]);
			}
		}
}
	for (int i = 0; i < n; i++)
	{
		if (i+1!= v)
		{
			cout << "(" << v << " -> " << i+1 << ")" << " = " << G[v-1][i] << endl;
		}
	}
	return 0;
}